<?php
/**
 * 领域上下文
 *
 * @date    2020-06-23 11:11:46
 * @version $Id$
 */

namespace App\Commons\Domain\Core;

abstract class DomainContext
{
    protected array $services = [];
    protected static $booted = [];
    protected $domainName;

    public function __construct()
    {
        // $this->domainName = str_replace('Context', '', class_basename($this)) . '.';
        static::bindings($this->services);
    }

    private static function bindings(array $services, string $domain = '')
    {
        if (isset(static::$booted[static::class])) {
            return;
        }
        static::$booted[static::class] = true;
        $container = app();
        foreach ($services as $key => $class) {
            if (is_numeric($key)) {
                continue;
            }
            $container->singletonIf(
                $domain . $key,
                function () use ($class) {
                    return resolve($class);
                }
            );
        }
    }

    public function __get($service)
    {
        return resolve($this->domainName . $service);
    }

    /**
     * 登录用户
     * @return [type] [description]
     */
    public function loginUser()
    {
        return auth()->user();
    }
}
